package loansManagement;

import java.util.Calendar;
import java.util.GregorianCalendar;

import main.Constraints;
import booksManagement.Book;
import dvdsManagement.Dvd;
import mediasManagement.Media;
import subscribersManagement.Subscriber;
import exceptions.*;

public class Validator {

public static int seniority(Calendar bornOrSuscribingDate){
		
		Calendar d1= new GregorianCalendar();
		Calendar d2= (Calendar) bornOrSuscribingDate.clone();
			
		int year=d1.get(Calendar.YEAR)-bornOrSuscribingDate.get(Calendar.YEAR);
		d2.set(Calendar.YEAR, d1.get(Calendar.YEAR));
		if(bornOrSuscribingDate.compareTo(d1)<=0){
			System.out.println(year);
			return year;
			
		}
		else{
			System.out.println(Math.max(0, year-1));
			return Math.max(0, year-1);}
	}
		
	

	public static boolean isAchild (Subscriber lender) throws BadParametersException{
		if (lender==null)
			throw new BadParametersException();
		return seniority(lender.getBornDate())<=Constraints.AGE_OF_CHILD;
		
	}
	
	public static boolean isOneYearSubscriber (Subscriber lender) throws BadParametersException{
		if (lender==null)
			throw new BadParametersException();
		return seniority(lender.getSubscriptionDate())<=1;	
	}
	

	public static boolean isTwoYearSubscriber (Subscriber lender) throws BadParametersException{
		if (lender==null)
			throw new BadParametersException();
		return seniority(lender.getSubscriptionDate())==2;	
	}

	public static boolean isSupThreeYearSubscriber (Subscriber lender) throws BadParametersException{
		if (lender==null)
			throw new BadParametersException();
		return seniority(lender.getSubscriptionDate())>=3;	
	}
	
	public static boolean canlendDvd (Subscriber lender) throws BadParametersException, ChildLoanException{
		if (isAchild(lender))
			throw new ChildLoanException();
			//return false;
		
		if(isOneYearSubscriber(lender)){
			return lender.getNumberOfDvdLoan() < Constraints.maxLOANS_OneYear_DVD;
			}	
		
		if(isTwoYearSubscriber(lender)){
			return lender.getNumberOfDvdLoan() < Constraints.maxLOANS_TwoYear_DVD;
			}
		
		if(isSupThreeYearSubscriber(lender)){
			return lender.getNumberOfDvdLoan() < Constraints.maxLOANS_supThreeYear_DVD;
			}
		
		return false;
	}
	

	public static boolean canlendBook (Subscriber lender) throws BadParametersException{
		
		if(isOneYearSubscriber(lender)){
			return lender.getNumberOfBookLoan()< Constraints.maxLOANS_OneYear_Book;
			}	
		
		if(isTwoYearSubscriber(lender)){
			return lender.getNumberOfBookLoan() < Constraints.maxLOANS_TwoYear_Book;
			}
		
		if(isSupThreeYearSubscriber(lender)){
			return lender.getNumberOfBookLoan() < Constraints.maxLOANS_supThreeYear_Book;
			}
		return false;
	}
	public static boolean canLend(Subscriber lender, Media media) throws BadParametersException, ChildLoanException{
		
		if (lender==null)
			throw new BadParametersException();
		
		
		if(media instanceof Dvd)
			return canlendDvd(lender);
		
		if(media instanceof Book)
			return canlendBook(lender);
		
		return false;
	
	}

		
		/**
		 */
		public boolean canReserve(Subscriber subscriber, Media media){
			return false;	
		}
}
